package com.ixuestar.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;

public class RandomStr {


    //随机数生成器
    private static Random random;
    //种子值
    private static long seed;

    //静态代码块，初始化种子值及随机数生成器
    static {
        seed = System.currentTimeMillis();
        random = new Random(seed);
    }


    /**
     * @设置种子值
     * @param s 随机数生成器的种子值
     */
    public static void setSeed(long s){
        seed = s;
        random = new Random(seed);
    }

    /**
     * @获取种子值
     * @return long 随机数生成器的种子值
     */
    public static long getSeed(){
        return seed;
    }

    /**
     * @随机返回0到1之间的实数 [0,1)
     * @return double 随机数
     */
    public static double uniform(){
        return random.nextDouble();
    }

    /**
     * @ 随机返回0到N-1之间的整数 [0,N)
     * @param N 上限
     * @return int 随机数
     */
    public static int uniform(int N){
        return random.nextInt(N);
    }

    /**
     * @ 随机返回0到1之间的实数 [0,1)
     * @return double 随机数
     */
    public static double random(){
        return uniform();
    }

    /**
     * @随机返回a到b-1之间的整数 [a,b)
     * @param a 下限
     * @param b 上限
     * @return int 随机数
     */
    public static int uniform(int a,int b){
        return a + uniform(b - a);
    }

    /**
     * @ 随机返回a到b之间的实数
     * @param a 下限
     * @param b 上限
     * @return double 随机数
     */
    public static double uniform(double a,double b){
        return a + uniform() * (b - a);
    }

    /**
     * @ 返回随机字符串，同时包含数字、大小写字母
     * @param len 字符串长度，不能小于3
     * @return String 随机字符串
     */
    public static String randomStr(int len){
        if(len < 3){
            throw new IllegalArgumentException("字符串长度不能小于3");
        }
        //数组，用于存放随机字符
        char[] chArr = new char[len];
        //为了保证必须包含数字、大小写字母
        chArr[0] = (char)('0' + uniform(0,10));
        chArr[1] = (char)('A' + uniform(0,26));
        chArr[2] = (char)('a' + uniform(0,26));


        char[] codes = { '0','1','2','3','4','5','6','7','8','9',
                'A','B','C','D','E','F','G','H','I','J',
                'K','L','M','N','O','P','Q','R','S','T',
                'U','V','W','X','Y','Z','a','b','c','d',
                'e','f','g','h','i','j','k','l','m','n',
                'o','p','q','r','s','t','u','v','w','x',
                'y','z','-'};
        //charArr[3..len-1]随机生成codes中的字符
        for(int i = 3; i < len; i++){
            chArr[i] = codes[uniform(0,codes.length)];
        }

        //将数组chArr随机排序
        for(int i = 0; i < len; i++){
            int r = i + uniform(len - i);
            char temp = chArr[i];
            chArr[i] = chArr[r];
            chArr[r] = temp;
        }

        return new String(chArr);
    }
    
    /**
     * @ 返回随机字符串，同时包含数字、大小写字母
     * @param len 字符串长度，不能小于3
     * @return String 随机字符串
     */
    public static String randomStr0(int len){
        if(len < 3){
            throw new IllegalArgumentException("字符串长度不能小于3");
        }
        //数组，用于存放随机字符
        char[] chArr = new char[len];
        //为了保证必须包含数字、大小写字母
        chArr[0] = (char)('0' + uniform(0,10));
        chArr[1] = (char)('A' + uniform(0,26));
        chArr[2] = (char)('a' + uniform(0,26));


        char[] codes = { '0','1','2','3','4','5','6','7','8','9',
                'A','B','C','D','E','F','G','H','I','J',
                'K','L','M','N','O','P','Q','R','S','T',
                'U','V','W','X','Y','Z','a','b','c','d',
                'e','f','g','h','i','j','k','l','m','n',
                'o','p','q','r','s','t','u','v','w','x',
                'y','z'};
        //charArr[3..len-1]随机生成codes中的字符
        for(int i = 3; i < len; i++){
            chArr[i] = codes[uniform(0,codes.length)];
        }

        //将数组chArr随机排序
        for(int i = 0; i < len; i++){
            int r = i + uniform(len - i);
            char temp = chArr[i];
            chArr[i] = chArr[r];
            chArr[r] = temp;
        }

        return new String(chArr);
    }
    
    /**
     * @ 返回随机字符串，同时包含数字、大小写字母
     * @param len 字符串长度，不能小于3
     * @return String 随机字符串
     */
    public static String randomNumber(int len){
        if(len < 3){
            throw new IllegalArgumentException("字符串长度不能小于3");
        }
        //数组，用于存放随机字符
        char[] chArr = new char[len];
        //为了保证必须包含数字、大小写字母
        chArr[0] = (char)('0' + uniform(0,10));


        char[] codes = { '0','1','2','3','4','5','6','7','8','9'};
        //charArr[3..len-1]随机生成codes中的字符
        for(int i = 0; i < len; i++){
            chArr[i] = codes[uniform(0,codes.length)];
        }

        //将数组chArr随机排序
        for(int i = 0; i < len; i++){
            int r = i + uniform(len - i);
            char temp = chArr[i];
            chArr[i] = chArr[r];
            chArr[r] = temp;
        }

        return new String(chArr);
    }
    
    public static void main(String[] args) {
    	String path = "E:\\image";
		for (int i = 0; i < 500; i++) {
			//定义二维码id
			String ypcId = "ypc" + "00000";
			if ((i+1)<10) {
				ypcId = ypcId + "00"+(i+1);
			}else if ((i+1)>9 && (i+1)<100) {
				ypcId = ypcId + "0"+(i+1);
			}else {
				ypcId = ypcId + i;
			}
			//定义二维码数字路径
			String code = randomNumber(10);
			String fileName = "barCode_"+code+".png";
			BarCodeUtil.getBarCode0(code, path, fileName);
			//生成图片
			String filePath = path + "\\" + fileName;
			File file = new File(filePath);
			String fileUpLoadQiniu = "";
			try {
				InputStream fileInputStream = new FileInputStream(file);
				String fileToBase64 = QiniuUpLoad.fileToBase64(fileInputStream);
				fileUpLoadQiniu = "https://cdn.travbao.com/"+new QiniuUpLoad().fileUpLoadQiniu(fileToBase64, fileName);
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			//定义日期
			String startDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
			Calendar instance = Calendar.getInstance();
			instance.add(Calendar.MONTH, 1);
			
			String stopDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(instance.getTime());
			System.out.println(ypcId+"\t"+"1"+"\t"+"-200.00"+"\t"+code+"\t"+fileUpLoadQiniu+"\t"+startDate+"\t"+stopDate
					+"\t"+"0"+"\t"+"1"+"\t"+"yas00000001"+"\t"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
		}
	}
}
